﻿using System;
using System.Collections.Generic;

namespace IniFileManager
{
	/// <summary>
	/// Generic storage for unique values (coparing by IComparable &lt;T &gt; interface). 
	/// UniqueList &lt;T &gt; can be also indexed by string (Elements are chosen by IComparable &lt;string &gt; inteface).
	/// </summary>
	/// <typeparam name="T">Stored items </typeparam>
	sealed public class UniqueList<T> : IEnumerable<T> where T : IComparable<string>, IComparable<T>
	{
		/// <summary>
		/// Local dynamic storage.
		/// </summary>
		private List<T> dynamicArray;

		/// <summary>
		/// Constructor of UniqueList
		/// </summary>
		public UniqueList()
		{
			this.dynamicArray = new List<T>();
		}

		/// <summary>
		/// Returns first element, which can be indexed by string "index".
		/// </summary>
		public T this[string index]
		{
			get
			{
				foreach( T item in this.dynamicArray )
				{
					if( item.CompareTo( index ) == 0 )
					{
						return item;
					}
				}

				// if we do not find item, item do not exist
				throw new ArgumentException( "This item does not exist." );
			}
		}

		#region visible List<T> Methods

		/// <summary>
		/// Add item to storage. Works only for 
		/// </summary>
		/// <param name="item">Added item.</param>
		public void Add( T item )
		{
			if( this.dynamicArray.Contains( item ) == true )
			{
				throw new ArgumentException( "UniqueList already contains item." );
			}

			this.dynamicArray.Add( item );
		}

		/// <summary>
		/// Remove item from storage.
		/// </summary>
		/// <param name="itemName">Removed item's name.</param>
		public void Remove( string itemName )
		{
			bool succesfullyRemoved = false;
			foreach( T item in this.dynamicArray )
			{
				if( item.CompareTo( itemName ) == 0 )
				{
					succesfullyRemoved = this.dynamicArray.Remove( item );

					break;
				}
			}

			if( succesfullyRemoved == false )
			{
				throw new ArgumentException( "Item does not exists." );
			}
		}

		/// <summary>
		/// Determines whether an element is in UniqueList.
		/// </summary>
		/// <param name="item">Object to locale in UniqueList.</param>
		/// <returns>Returns true if cointains. Returns false otherwise</returns>
		public bool Contains( T item )
		{
			return this.dynamicArray.Contains( item );
		}

		/// <summary>
		/// Remove item from storage.
		/// </summary>
		/// <param name="item">Removed item.</param>
		public void Remove( T item )
		{
			bool succesfullyRemoved = this.dynamicArray.Remove( item );
			if( succesfullyRemoved == false )
			{
				throw new ArgumentException( "Item does not exists." );
			}
		}

		#endregion



		#region IEnumerable<T> Members

		/// <summary>
		/// Get enumerator metho
		/// </summary>
		/// <returns></returns>
		public IEnumerator<T> GetEnumerator()
		{
			return this.dynamicArray.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return this.dynamicArray.GetEnumerator();
		}

		#endregion
	}
}
